let MyPromise = require('./MyPromise');

// MyPromise = Promise;


// 练习手写Promise
// 需求1,测试同步执行
const p1 = new MyPromise((resolve, reject) => {
    resolve('p1成功');
    // reject("p1失败");
});

p1.then(console.log, console.log);

console.log("链式是否出现?:")
p1.then(() => "链式").then(console.log)

p1.then(() => {
    console.log('我是测试失败链式')
    throw new Error('测试失败')
}).then(() => "链式", (reason) => {
    console.error("上一个失败后", reason.message)
    return "失败处理后变成功"
}).then((value) => {
    console.log("出来吗", value);
    throw new Error("catch一下吧")
}).catch((r) => {
    console.log("catch里面有什么东西", r.message)
})

const other = () => new MyPromise((resolve, reject) => {
    resolve("other");
})

p1.then(() => other()).then(console.log)

const c1 = p1.then(() => c1);
c1.then(console.log, r => console.error(r.message));

const p2 = new MyPromise((resolve, reject) => {
    // resolve('p1resolve');
    setTimeout(() => resolve("p2成功"), 100);
    // setTimeout(() => reject("p2失败"), 100);
});

p2.then(console.log, console.log);
p2.then(console.log, console.log);
p2.then(console.log, console.log);


const pc = p2.then(() => pc, console.log);

const perror = new MyPromise(() => {
    throw new Error("error")
});

perror.then().then(console.log, r => console.error(r.message))


// all测试

MyPromise.all(['a', 'b', p1, p2]).then(console.log);
MyPromise.all(['a', 'b', p1, p2]).finally(console.log);

MyPromise.resolve("resolve方法").finally(console.log).then(console.log);

MyPromise.reject("我被reject了吗").then(console.log,console.error);

MyPromise.race(["我先race","我没race"]).then(console.log,console.error);